﻿/*----------------------------------------------------------------
//  <copyright file="Snowflake32KeyGenerator.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2022-11-14 11:08</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.Entity.KeyGenerator
{
    #region "Int32雪花算法主键生成器"
    /// <summary>
    /// Int32雪花算法主键生成器
    /// </summary>
    public class Snowflake32KeyGenerator : ISnowflakeKeyGenerator<int>
    {
        //字段
        private MicroCloudOptions _options;

        #region "构造函数"
        #region "初始化一个<see cref="Snowflake32KeyGenerator"/>类型的新实例"
        /// <summary>
        /// 初始化一个<see cref="Snowflake32KeyGenerator"/>类型的新实例
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public Snowflake32KeyGenerator(IServiceProvider provider)
        {
            _options = provider.GetMicroCloudOptions();
            if (_options.Snowflake.WorkerId > 0)
            { }
            if (_options.Snowflake.StartTimeStampUtc > 0)
            {
                SnowflakeIdGeneratorSimple.Twepoch = (_options.Snowflake.StartTimeStampUtc / 1000 / 60).CastTo<int>();
            }
        }
        #endregion

        #endregion

        #region "方法"
        #region "获取一个<see cref="int"/>类型的主键数据"
        /// <summary>
        /// 获取一个<see cref="int"/>类型的主键数据
        /// </summary>
        /// <returns></returns>
        public int Create()
        {
            return SnowflakeIdGeneratorSimple.NewId();
        }
        #endregion

        #endregion

    }
    #endregion

}
